package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.dey.entity.Result;
import org.dey.mapper.RiskMapper;
import org.dey.pojo.PageVo;
import org.dey.pojo.Risk;
import org.dey.service.RiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RiskServiceImpl extends ServiceImpl<RiskMapper, Risk> implements RiskService {
    @Autowired
    RiskMapper riskMapper;

    @Override
    public PageVo<Risk> queryR(Risk risk, int pageNum, int pagesize) {

        QueryWrapper<Risk> queryWrapper = new QueryWrapper<Risk>();
        if (StringUtils.isNotBlank(risk.getRname())) {
            queryWrapper.or().like("rname", risk.getRname());
        }
        if (StringUtils.isNotBlank(risk.getFileNumbe())) {
            queryWrapper.or().like("fileNumbe", risk.getFileNumbe());
        }
        PageHelper.startPage(pageNum, pagesize);

        List<Risk> list = this.list(queryWrapper);

        PageInfo<Risk> pageInfo = new PageInfo<>(list);

        PageVo<Risk> pageVo = new PageVo<>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public Map addR(Risk risk) {
        Boolean num = this.save(risk);

        Map map = new HashMap();
        map.put("code", "0");
        map.put("msg", "添加失败");
        if (num) {
            map.put("code", "1");
            map.put("msp", "添加成功");
        }
        return map;
    }

    @Override
    public Result deleteR(int id) {
        Result result = null;

        return null;
    }

    @Override
    public int updateR(Risk risk) {
        int num = 0;
        boolean b = this.updateById(risk);
        if (b) {
            num = 1;
        }
        return num;
    }
}
